const mongoose = require('mongoose');

module.exports={
    addCount:async function(options){
        let FriendLinkClickModel=mongoose.models['FriendLinkClick'];
        let addDate=new Date().toLocaleDateString();
        if(addDate.indexOf('/') > -1){
            let date = new Date();
            addDate =  date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + (date.getDate());
        }
        let friendLinkClick=await FriendLinkClickModel.findOne({appId:options.appId,sceneName:options.sceneName,area:options.area,appName:options.appName,addDate:addDate});
        if(!friendLinkClick){
            friendLinkClick = await FriendLinkClickModel.create({
                appId:options.appId,
                area:options.area,
                sceneName:options.sceneName,
                appName:options.appName,
                addDate:addDate,
                count:1,
                openCount:0,
                newCount:0,
                newOpenCount:0
            })
        }else{
            friendLinkClick.count++;
            await friendLinkClick.save();
        }

        // 点击去重
        if(options.appId && options.self.clickFLs && options.self.clickFLs.indexOf(options.appId) == -1){
            friendLinkClick.newCount += 1;
            await friendLinkClick.save();

            options.self.clickFLs.push(options.appId);
        }
    },
    addOpenCount:async function(options){
        let FriendLinkClickModel=mongoose.models['FriendLinkClick'];
        let addDate=new Date().toLocaleDateString();
        if(addDate.indexOf('/') > -1){
            let date = new Date();
            addDate =  date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + (date.getDate());
        }
        let friendLinkClick=await FriendLinkClickModel.findOne({appId:options.appId,sceneName:options.sceneName,area:options.area,appName:options.appName,addDate:addDate});

        if(!friendLinkClick){
            friendLinkClick = await FriendLinkClickModel.create({
                appId:options.appId,
                area:options.area,
                sceneName:options.sceneName,
                appName:options.appName,
                addDate:addDate,
                count:0,
                openCount:1,
                newCount:0,
                newOpenCount:0
            })
        }else{
            friendLinkClick.openCount++;
            await friendLinkClick.save();
        }

        // 打开去重
        if(options.appId && options.self.openFLs && options.self.openFLs.indexOf(options.appId) == -1){
            friendLinkClick.newOpenCount += 1;
            await friendLinkClick.save();

            options.self.openFLs.push(options.appId);
        }


        //加入分渠道的点击事件统计
        if(!!options.self.referrerInfoAppId){
            let ChannelEventModel=mongoose.models['ChannelEvent'];
            //视频观看成功事件
            let now=new Date();
            let nowTick=now.getTime();
            ChannelEventModel.create({
                channelId:options.self.referrerInfoAppId,
                addTimeTick:nowTick,
                eventName:'打开友情链接'
            });
            //新用户事件
            let registerDate=new Date(options.self.registerTimeTick);
            if(now.toLocaleDateString()===registerDate.toLocaleDateString()){
                ChannelEventModel.create({
                    channelId:options.self.referrerInfoAppId,
                    addTimeTick:nowTick,
                    eventName:'新用户打开友情链接'
                });
            }
        }
    }
};